package com.behome.netty.netty_B.bean;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.behome.netty.common.ByteConventerKit;
import com.behome.netty.netty_B.enums.ReBackAnnotation;
import com.behome.netty.netty_B.enums.ReBackCmdType;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * @author bh_ljy
 * @description: 终端播发记录
 * @date: 2024/5/27 10:48
 */

@ReBackAnnotation(
        type = ReBackCmdType.BROADCAST_RECORDS
)
public class TerminalBroadcastRecords extends BusinessReBackBase {
    private byte resourceCodeLength = 12;
    private String resourceCode;
    private byte broadcastRecordCount;
    private BroadcastRecords[] broadcastRecords;

    public TerminalBroadcastRecords() {
    }

    public byte[] serialize() {
        return null;
    }

    public BusinessReBackBase deserialize(byte[] businessData) {
        if (businessData != null && businessData.length >= 14) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(businessData);
            this.setResourceCodeLength(byteBuffer.get());
            byte[] resourceCodeData = new byte[this.getResourceCodeLength()];
            byteBuffer.get(resourceCodeData);
            this.setResourceCode(ByteConventerKit.deserializeResourceCode(resourceCodeData));
            this.setBroadcastRecordCount(byteBuffer.get());
            if (this.getBroadcastRecordCount() > 0) {
                BroadcastRecords[] records = new BroadcastRecords[this.getBroadcastRecordCount()];
                for (int i = 0; i < this.getBroadcastRecordCount(); ++i) {
                    BroadcastRecords record = new BroadcastRecords();
                    record.setChannelId(byteBuffer.getShort());
                    record.setBroadcastStatus(byteBuffer.get());
                    byte[] ebmIdData = new byte[18];
                    byteBuffer.get(ebmIdData);
                    String ebmId = ByteConventerKit.deserializeEbmId(ebmIdData);
                    record.setEbmId(ebmId);
                    record.setTaskType(byteBuffer.get());
                    record.setBroadcastType(byteBuffer.get());
                    record.setEventLevel(byteBuffer.get());
                    byte[] eventTypeData = new byte[5];
                    byteBuffer.get(eventTypeData);
                    String eventType = new String(eventTypeData, StandardCharsets.US_ASCII);
                    record.setEventType(eventType);
                    byte[] beginTimeData = new byte[4];
                    byteBuffer.get(beginTimeData);
                    record.setBeginTime(ByteConventerKit.bytesToDatetime(beginTimeData));
                    byte[] endTimeData = new byte[4];
                    byteBuffer.get(endTimeData);
                    record.setEndTime(ByteConventerKit.bytesToDatetime(endTimeData));
                    if (record.getTaskType() == 3 || record.getTaskType() == 4) {
                        record.setPhoneLength(byteBuffer.get());
                        byte[] phoneData = new byte[record.getPhoneLength()];
                        byteBuffer.get(phoneData);
                        record.setPhone(ByteConventerKit.bcdToStr(phoneData));
                    }

                    record.setVolume(byteBuffer.get());
                    record.setResourceCodeCount(byteBuffer.get());
                    record.setResourceCodeLength(byteBuffer.get());
                    if (record.getResourceCodeCount() > 0) {
                        String[] resourceCodes = new String[record.getResourceCodeCount()];
                        for (int j = 0; j < resourceCodes.length; ++j) {
                            byte[] tmpResourceCodeData = new byte[record.getResourceCodeLength()];
                            byteBuffer.get(tmpResourceCodeData);
                            resourceCodes[j] = ByteConventerKit.deserializeResourceCode(tmpResourceCodeData);
                        }
                        record.setResourceCodes(resourceCodes);
                    }
                    records[i] = record;
                }
                this.setBcCastRecords(records);
            }
        }

        return this;
    }

    public byte getResourceCodeLength() {
        return this.resourceCodeLength;
    }

    public void setResourceCodeLength(byte resourceCodeLength) {
        this.resourceCodeLength = resourceCodeLength;
    }

    public String getResourceCode() {
        return this.resourceCode;
    }

    public void setResourceCode(String resourceCode) {
        this.resourceCode = resourceCode;
    }

    public byte getBroadcastRecordCount() {
        return this.broadcastRecordCount;
    }

    public void setBroadcastRecordCount(byte broadcastRecordCount) {
        this.broadcastRecordCount = broadcastRecordCount;
    }

    public BroadcastRecords[] getBcCastRecords() {
        return this.broadcastRecords;
    }

    public void setBcCastRecords(BroadcastRecords[] broadcastRecords) {
        this.broadcastRecords = broadcastRecords;
    }

    public boolean validateBeforeSerialize() {
        return true;
    }

    public String toCHJsonString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("资源编码长度", this.resourceCodeLength);
        jsonObject.putOnce("前端资源编码", this.resourceCode);
        jsonObject.putOnce("播发记录数", this.broadcastRecordCount);
        JSONArray jsonArray = new JSONArray();
        if (this.broadcastRecords != null) {
            for (BroadcastRecords broadcastRecord : this.broadcastRecords) {
                jsonArray.put(broadcastRecord.toCHJsonString());
            }
        }
        jsonObject.putOnce("播发记录", jsonArray);
        return jsonObject.toString();
    }
}
